package com.hmys.api.controller;

import com.hmys.api.model.WebExchangeRate;
import com.hmys.api.model.WebHousePriceRentTrend;
import com.hmys.api.response.Result;
import com.hmys.api.response.ResultCode;
import com.hmys.api.service.WebExchangeRateService;
import com.hmys.api.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by chen365 on 2018/03/15.
 */
@RestController
@Api(tags = "汇率")
public class ExchangeRateController {

    @Autowired
    private WebExchangeRateService webExchangeRateService;

    @ApiOperation(value = "获取汇率", notes = "获取汇率")
    @GetMapping(value = "/exchange/rate/list")
    public Object ExchangeRateList() {
        List<WebExchangeRate> list = webExchangeRateService.selectWebExchangeRate();
        if (list.size() > 0) {
            return new Result(ResultCode.SUCCESS, list);
        }
        return new Result(ResultCode.NO_DATA_WAS_FOUND);
    }


    @ApiOperation(value = "汇率走势", notes = "汇率走势", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "currencyId", value = "类型，1316,美元，1329,泰国铢", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "btime", value = "开始日期", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "etime", value = "结束日期", required = false, paramType = "query")
    })
    @GetMapping(value = "/exchange/rate/list/{currencyId}")
    public Object trend(@PathVariable("currencyId") Long currencyId, String btime, String etime) {
        WebExchangeRate record = new WebExchangeRate();
        if(StringUtils.isEmpty(currencyId)){
            return new Result(ResultCode.ERROR_PARAM);
        }
        record.setCurrencyId(currencyId);
        if(!StringUtils.isEmpty(btime) && !StringUtils.isEmpty(etime)) {
            if (DateUtils.getDateByStr(btime).compareTo(DateUtils.getDateByStr(etime)) >= 0) {
                return new Result(ResultCode.ERROR_PARAM);
            }
            record.setCreateTime(DateUtils.getDateByStr(btime));
            record.setUpdateTime(DateUtils.getDateByStr(etime));
        }
        record.setStatus(true);
        List<WebExchangeRate> list = webExchangeRateService.selectExchangeRateByBean(record);
        if (list.size() > 0) {
            return new Result(ResultCode.SUCCESS, list);
        } else {
            return new Result(ResultCode.NOT_FOUND);
        }
    }
}
